package com.appspot.pro_log.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Pro_Log implements EntryPoint
{
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String			SERVER_ERROR		= "An error occurred while "
																					+ "attempting to contact the server. Please check your network "
																					+ "connection and try again.";
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync	servlet	= GWT.create(GreetingService.class);
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad()
	{
		final Button sendButton = new Button("Load");
		final TextBox urlField = new TextBox();
		urlField.setText("http://google.pl");
		urlField.setWidth("800");
		
		// We can add style names to widgets
		sendButton.addStyleName("sendButton");
		
		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element				
		RootPanel.get("url").add(urlField);
		RootPanel.get("sendRequestButton").add(sendButton);

		
		// Focus the cursor on the name field when the app loads
		urlField.setFocus(true);
		urlField.selectAll();		
	
		final HTML loadedHTML = new HTML();	
		RootPanel.get("loadedHTML").add(loadedHTML);
		
		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler
		{
			public void onClick(ClickEvent event)
			{				
				sendRequest();
			}

			public void onKeyUp(KeyUpEvent event)
			{
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
				{
					sendRequest();
				}
			}

			private void sendRequest()
			{
				sendButton.setEnabled(false);
				String textToServer = urlField.getText();			
				loadedHTML.setHTML("Loading ...");
				servlet.greetServer(textToServer, new AsyncCallback<String>()
				{
					public void onFailure(Throwable caught)
					{
						loadedHTML.setHTML(caught.getLocalizedMessage());
					}
					
					public void onSuccess(String result)
					{
						loadedHTML.setHTML(result);
					}
				});
				sendButton.setEnabled(true);
			}
		}
		
		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		urlField.addKeyUpHandler(handler);
	}
}
